import storage from '@/utils/storage'
import router from '@/router'
const state = {
  isLogin: storage.session.get('is-login') || storage.get('is-login')
}
const getters = {
  isLogin: state => state.isLogin // 页面加载状态
}
const mutations = {
  /**
   * 设置登录
   * @param state
   */
  login: (state, data) => {
    state.isLogin = true
    if (data === true) {
      storage.set('is-login', true)
      return
    }
    storage.session.set('is-login', true)
  },
  /**
   * 设置退出
   * @param state
   */
  logout: async state => {
    state.isLogin = false
    storage.session.del('is-login')
    storage.del('is-login')
    await router.replace("/login")
  }
}
const actions = {
  /**
   * 登录
   * @param commit
   * @param payload {boolean} 是否记住登录状态
   */
  login({ commit }, payload) {
    commit('login', payload)
  },
  /**
   * 退出
   * @param commit
   */
  logout({ commit }) {
    commit('logout')
  }
}
export default {
  state,
  mutations,
  actions,
  getters
}
